<template>
	<view class="mainpadding" v-if="pageShow">
		<view class="flexcenter margin_top5" @click="uploadImg(1,avatar,avatar_text)">
			<view class="touxiang flexcenter">
				<image :src="avatar_text" mode=""></image>
				<view class="toubox flexcenter">
					<u-icon name="camera" color="#fff" size="38"></u-icon>
				</view>
			</view>
		</view>
		<view class="">
			<view class="flexbetween margin_top6" @click="jumper('/pages/mine/xiugainc?name=',nickname)">
				<view class="sanshier xiaohui">昵称</view>
				<view class="flexright">
					<view class="sanshier margin_right1">{{nickname}}</view>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="flexbetween margin_top6">
				<view class="sanshier xiaohui">手机号</view>
				<view class="sanshier">{{userinfo.mobile}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				avatar: "",
				avatar_text: "",
				nickname: "",
				userinfo:{},
				gender: "",
				pageShow:true,
			}
		},
		onShow() {
			uni.$off("editname")
			uni.$on("editname", data => {
				this.nickname = data
			})
			this.init()
		},
		methods: {
			init() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
					this.userinfo = res.data
					this.pageShow = true
				})
			},
			uploadImg() {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						if(res.tapIndex==0){
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i])
								}
							}).catch(err => {});
						}else{
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i])
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					console.log(res);
					this.avatar = res.url;
					this.avatar_text = res.fullurl
					this.setinfo({
						avatar: res.url
					})
				}).catch(err => {});
			},
			setinfo(data) {
				httpRequest.request('/api/user/profile', 'GET', data).then(res => {
					if(res.code!=1){
						httpRequest.toast(res.msg)
					}
				})
			},
			jumper(url, text) {
				uni.navigateTo({
					url: url + text
				})
			},
		}
	}
</script>

<style lang="scss">
	/deep/.u-radio-group {
		flex: none !important;
	}
	.touxiang {
		width: 186rpx;
		height: 186rpx;
		border-radius: 50%;
		position: relative;
		z-index: 100;

		image {
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}
	}

	.toubox {
		position: absolute;
		left: 0;
		top: 0;
		width: 186rpx;
		height: 186rpx;
		background: #000000;
		opacity: .3;
		opacity: 0.5;
		border-radius: 50%;
	}
</style>